package jdbc.task1;

import java.sql.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Scanner;

/*
2：在控制台输入一个学生的全部信息，保存到数据库，
注意日期的处理(Statement)
 */
public class Task2 extends DbUtil_2 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Task2 t = new Task2();

        System.out.print("输入学号：");
        int stuno = input.nextInt();
        System.out.print("输入姓名：");
        String sname = input.next();
        System.out.print("输入性别：");
        String sex = input.next();
        System.out.print("输入电话：");
        String telephone = input.next();
        System.out.print("输入qq：");
        String qq = input.next();
        System.out.print("输入微信：");
        String wechat = input.next();
        System.out.print("输入年级：");
        int gradeid = input.nextInt();
        System.out.print("输入入学日期（格式：yyyy-MM-dd）：");
        String enterdate1 = input.next();
        System.out.print("输入来自城市：");
        String fromcity = input.next();
        System.out.print("输入生日（格式：yyyy-MM-dd）：");
        String birthday1 = input.next();
        System.out.print("输入出生地址：");
        String address = input.next();
        System.out.print("输入密码：");
        String pass = input.next();

        DateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        Date enterdate = null;
        Date birthday = null;
        try {
            enterdate = new Date(sim.parse(enterdate1).getTime());
            birthday = new Date(sim.parse(birthday1).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }


        String sql = "insert into s_student values(" + stuno + ",'" + sname + "','" + sex + "','" + telephone + "','" + qq + "','" + wechat + "'," + gradeid + ",'"
                + enterdate + "','" + fromcity + "','" + birthday + "','" + address + "','" + pass + "',DEFAULT)";

        Connection con = t.getConnection();
        Statement stmt = null;
        try {
            stmt = con.createStatement();
            int rows = stmt.executeUpdate(sql);
            System.out.println("学生信息已保存到数据库！");
            System.out.println("成功插入" + rows + "行数据");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            t.close(stmt);
            t.close(con);
        }


    }
}
